package com.lepu.app.fun.my.adapter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.bbs.BbsDetailActivity;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.MyMassegeBbsDetailActivity;
import com.lepu.app.fun.my.bean.BeanMsgBbs;
import com.lepu.pasm.R;

public class AdapterMyMassegeBbs extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private MyMassegeBbsDetailActivity mContext = null;
	private ArrayList<BeanMsgBbs> mArrayList = null;

	public AdapterMyMassegeBbs(MyMassegeBbsDetailActivity context, ArrayList<BeanMsgBbs> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "InflateParams", "SimpleDateFormat" })
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_massege_bbs_item, null);

			holder = new ViewHolder();

			LinearLayout itemLayout = (LinearLayout) convertView.findViewById(R.id.itemLayout);
			ImageView newImage = (ImageView) convertView.findViewById(R.id.newImageView);
			CircularImageView iconImage = (CircularImageView) convertView.findViewById(R.id.iconImageView);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
			TextView timeTextView = (TextView) convertView.findViewById(R.id.timeTextView);
			TextView contentTextView = (TextView) convertView.findViewById(R.id.contentTextView);
			TextView replyTextView = (TextView) convertView.findViewById(R.id.replayName);
			TextView replyNameTextView = (TextView) convertView.findViewById(R.id.replyNameTextView);
			TextView replyContentTextView = (TextView) convertView.findViewById(R.id.replyContentTextView);
			
			holder.itemLayout = itemLayout;
			holder.newImage = newImage;
			holder.iconImage = iconImage;
			holder.nameTextView = nameTextView;
			holder.timeTextView = timeTextView;
			holder.replyTextView = replyTextView;
			holder.contentTextView = contentTextView;
			holder.replyNameTextView = replyNameTextView;
			holder.replyContentTextView = replyContentTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMsgBbs item = mArrayList.get(position);
		// 头像
		String imageUrl = Setting.getHeadIconUrl(item.ReplyUserID);
		UniversalImageLoadTool.disPlay(imageUrl, holder.iconImage,R.drawable.app_ic_default_avatar);

		// time
		long timeLong = Long.valueOf(item.RecordCreateTime)*1000;
		SimpleDateFormat sdf=new SimpleDateFormat(DateUtilBase.DATE_ALL_ALL);  
		  
		String timeString = sdf.format(new Date(timeLong)); 
		
		holder.timeTextView.setText(DateUtilBase.friendly_time(timeString));
		
		//new 
		if(BeanIndexNew.isExist(item.MessageID)){
			holder.newImage.setVisibility(View.INVISIBLE);
		}else{
			holder.newImage.setVisibility(View.VISIBLE);
		}
		
		if(item.Abstract.contains("subject")){
			holder.replyTextView.setText("回复了"+item.BeReplyUserNickname);
			String[] absArray = item.Abstract.replaceAll("subject", "").replaceAll("\\[", "").replaceAll("\\]", "").split("\\/");
			holder.replyNameTextView.setText(absArray[0]);
			holder.replyContentTextView.setText(absArray[1]);
			holder.replyContentTextView.setVisibility(View.VISIBLE);
		}else{
			holder.replyTextView.setText("回复了我");
			holder.replyNameTextView.setText(item.Abstract);
			holder.replyContentTextView.setVisibility(View.GONE);
		}
		
		holder.nameTextView.setText(item.ReplyUserNickname);
		holder.contentTextView.setText(item.ReplyContent);
		
		holder.itemLayout.setOnClickListener(new ViewClick(item, holder.newImage));

		return convertView;
	}
	
	class ViewClick implements View.OnClickListener{
		public BeanMsgBbs bean;
        public ImageView imageView;
		
		public ViewClick(BeanMsgBbs bean, ImageView imageView){
			this.imageView = imageView;
			this.bean = bean;
		}
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.itemLayout:
				Intent intent = new Intent(mContext,BbsDetailActivity.class);
				intent.putExtra("threadId", bean.ThreadID);
				mContext.startActivity(intent, true);
				
				BeanIndexNew.insertTargetId(bean.MessageID);
				imageView.setVisibility(View.INVISIBLE);
				break;

			default:
				break;
			}
		}
		
	}

	 static class ViewHolder {
		LinearLayout itemLayout;
		
		ImageView newImage;
		CircularImageView iconImage;
		TextView nameTextView;
		TextView timeTextView;
		TextView contentTextView;
		TextView replyTextView;
		TextView replyNameTextView;
		TextView replyContentTextView;
		
	}
}
